import EXIF from 'exif-js'
import ImgFile from "@/model/ImgFile"
import BalaBala from "@/methods/BalaBala"
import axios from "@/plugins/axios"
import User from "@/model/User"

export default class ImgUtils {
  public static readonly imgUrl: string = process.env.VUE_APP_BASE + 'img/'

  public static async getImgObj(target: HTMLInputElement): Promise<ImgFile> {
    return new Promise<ImgFile>((resolve, reject) => {
      //得到文件
      let imgFile: File = target.files[0]
      const imgSize = imgFile.size
      if (imgSize / 1024 / 1024 > 10) {
        BalaBala.error('图片大小不能超过10MB！')
        reject()
      }
      const reader = new FileReader()
      // 得到文件，先给的假的scr
      let imgObj: ImgFile = new ImgFile(imgFile)
      EXIF.getData(imgFile, () => {
        //获取照片转动值
        const Orientation = EXIF.getTag(imgFile, 'Orientation')
        reader.readAsDataURL(imgFile)
        reader.onload = (event: any) => {
          let imgSrc: string = event.target.result
          if (Orientation && Orientation !== 1) {
            const uploadBase64 = new Image()
            uploadBase64.src = imgSrc
            uploadBase64.onload = () => {
              //修正旋转图片
              const imgWidth = uploadBase64.width
              const imgHeight = uploadBase64.height
              const canvas = document.createElement("canvas")
              const ctx = canvas.getContext("2d")
              canvas.width = imgWidth
              canvas.height = imgHeight
              ctx.drawImage(uploadBase64, 0, 0, imgWidth, imgHeight)
              switch (Orientation) {
                case 6:
                  canvas.width = imgHeight
                  canvas.height = imgWidth
                  //旋转90度 Math.PI=180度
                  ctx.rotate(Math.PI / 2)
                  ctx.drawImage(uploadBase64, 0, -imgHeight)
                  break
                case 8:
                  //旋转180
                  ctx.rotate(Math.PI)
                  ctx.drawImage(uploadBase64, -imgWidth, -imgHeight)
                  break
                case 3:
                  //旋转270度
                  canvas.width = imgHeight
                  canvas.height = imgWidth
                  ctx.rotate(Math.PI * 3 / 2)
                  ctx.drawImage(uploadBase64, -imgWidth, 0)
                  break
              }
              imgObj.src = canvas.toDataURL(imgFile.type)
              // 必须分开写resolve，因为方法是异步的，否则会导致无法显示图片
              resolve(imgObj)
            }
          } else {
            //赋值真实的src
            // 必须分开写resolve，因为方法是异步的，否则会导致无法显示图片
            imgObj.src = imgSrc
            resolve(imgObj)
          }
        }
      })
    })
  }

  public static uploadImgAPI(uploadImg: File, type: string): Promise<any> {
    const formData = new FormData()
    formData.append("file", uploadImg)
    formData.append("type", type)
    return axios.post('/upload/img', formData)
  }

  public static getTalkLargeImgUrl(user: User): string {
    return ImgUtils.imgUrl + 'talk/' + user.id + '/normal/'
  }

  public static getTalkSmallImgUrl(user: User): string {
    return ImgUtils.imgUrl + 'talk/' + user.id + '/thumbnail/'
  }
}
